home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 1
/
Nebula One.iso
/
Financial
/
USDebtWatch
/
unix-version
/
xdebt92.c
< prev
Wrap
C/C++ Source or Header
|
1995-06-12
|
6KB
|
214 lines
/* Displays the current national debt in a window, updated once a second.
Public domain. By Jamie Zawinski <jwz@lucid.com>.
(you may need to add -DSYSV if timelocal is undefined -- tom)
cc -O -o xdebt xdebt-new.c -lXaw -lXmu -lXt -lX11 -lXext
More mods by sonroc@nemesis.zycad.com [a/k/a twk@zycad.com] to include
per-capita, also small square vs big rect; changed number printing style.
Theoretically, you can run it as:
xdebt [-g +XCOORD+YCOORD] [-update SECS] [-font FONT]
I haven't tested that -font works, but I use
xdebt -g +1080+360
routinely and
xdebt -update 1
also works for me. Your mileage may vary....
--sonroc
More mods by Perry Metzger, (pmetzger@shearson.com); reverted to a
big rectangle, eliminated silly cents field, uped the updates to
once a second, picked a bigger font, changed the labels to suit my
tastes. Easy to change, though.
note by Erik Sowa (sowa@netcom.com)
ultrix 4.2 compilation command:
cc -DSYSV -O -o xdebt92 xdebt92.c -I/usr/include/mit -lXaw -lXmu -lXt -lX11 -lXext
*/
#include <stdio.h>
#include <X11/Xos.h>
#include <X11/Intrinsic.h>
#include <X11/Xaw/Label.h>
#ifdef SYSV
#define timelocal mktime
#endif
typedef double debt_t;
typedef double pop_t;
/* The US Population, and its rate of increase, as of July 1, 1990 */
/* (in people/second) based on numbers from the CIA's World Factbook 1990 */
/* .... thanks to tom@genie.slhs.udel.edu */
#define POP 250410000.0 /* people */
#define POP_DELTA 0.0714151266 /* increase in people per second */
static struct tm pop_tm = { 0, 0, 0, 1, 6, 90 };
/* The US National Debt, and its rate of increase as of June 30, 1992 */
/* (in $/second) based on numbers from the 5 July 1992 Albuquerque Journal */
/* .... thanks to Brooke King brooke@fuchsia.albuq.ingr.com */
#define DEBT 3965170506502.395 /* in dollars */
#define DEBT_DELTA 14132.887 /* Dollars per second */
static struct tm debt_tm = { 0, 0, 0, 30, 05, 92 };
debt_t
national_debt_at (now)
time_t now;
{
time_t debt_date = timelocal (&debt_tm);
time_t seconds_since_then = now - debt_date;
debt_t delta_since_then = DEBT_DELTA * seconds_since_then;
return DEBT + delta_since_then;
}
pop_t
national_pop_at (now)
time_t now;
{
time_t pop_date = timelocal (&pop_tm);
time_t seconds_since_then = now - pop_date;
pop_t delta_since_then = POP_DELTA * seconds_since_then;
return POP + delta_since_then;
}
void
pretty_number(n, buf)
double n;
char buf[256];
{
char tmpBuf[256], *cp, *bp;
int len, count, digits;
/* sprintf(tmpBuf, "%.2lf", n);*/
sprintf(tmpBuf, "%.0lf", n);
len = strlen(tmpBuf);
cp = tmpBuf + len - 1;
bp = buf;
/* copy over the cents and first 3 digits */
/* *bp++ = *cp--; *bp++ = *cp--; *bp++ = *cp--; */
*bp++ = *cp--; *bp++ = *cp--; *bp++ = *cp--;
digits = 7;
/* Now every three digits gets a comma; at 10 digits insert a line at the
comma */
while (cp >= tmpBuf)
{
/* MODIFY NEWLINE IN LARGE NUMBERS HERE */
/* if (digits > 10)
{
*bp++ = ' '; *bp++ = '\n'; digits=0;
}*/
*bp++ = ','; digits++;
for (count = 0; count < 3 && cp >= tmpBuf; count++)
{
*bp++ = *cp--; digits++;
}
}
*bp++ = '\0';
strcpy(tmpBuf, buf);
len = strlen(tmpBuf);
for (count = 0; count < len; count++)
{
buf[(len-1) - count] = tmpBuf[count];
}
buf[len] = '\0';
}
void
construct_string (now, outBuf)
time_t now;
char *outBuf;
{
debt_t debt = national_debt_at(now);
pop_t pop = national_pop_at(now);
char debtBuf[256], shareBuf[256];
pretty_number(debt, debtBuf);
pretty_number(debt/pop, shareBuf);
sprintf(outBuf, "U.S. National Debt:\n$%s\nYour Share:\n$%s", debtBuf, shareBuf);
}
static int update;
/* MODIFY FONT HERE */
static char *defaults[] = {
/* "*Label.font: 12x24",/* /* was times-...-240 */
/* "*Label.font: *-helvetica-bold-r-*-*-*-100-*-*-*-*-*-*", /* was times-...-240 */
"*Label.font: *-times-bold-r-*-*-*-180-*-*-*-*-*-*",
"*title: xdebt92",
"*update: 1",
NULL
};
static XrmOptionDescRec options [] = {
{ "-update", "*update", XrmoptionSepArg, 0 }
};
static void
get_update (dpy) /* Easier than making a subclass... */
Display *dpy;
{
char *name, *class, *type, buf1[255], buf2[255];
XrmValue value;
XtGetApplicationNameAndClass (dpy, &name, &class);
sprintf (buf1, "%s.update", name);
sprintf (buf2, "%s.Update", class);
XrmGetResource (XtDatabase (dpy), buf1, buf2, &type, &value);
if (sscanf (value.addr, " %d ", &update) == 0 || update < 1)
{
fprintf (stderr, "%s: update must be a positive integer, not \"%s\"\n",
name, value.addr);
update = 1;
}
}
static void
timer (w, id)
Widget w;
XtIntervalId id;
{
char buf [255];
Arg av [10];
int ac = 0;
time_t now = time((time_t *)0);
construct_string (now, buf);
XtSetArg (av [ac], XtNlabel, buf); ac++;
XtSetValues (w, av, ac);
XtAppAddTimeOut (XtWidgetToApplicationContext (w), update * 1000, timer, w);
}
void
main (argc, argv)
int argc;
char **argv;
{
XtAppContext app;
Widget shell = XtAppInitialize (&app, "XDebt", options, XtNumber (options),
&argc, argv, defaults, NULL, 0);
Widget label;
XEvent event;
if (argc > 1)
{
fprintf (stderr, "%s: unknown option %s\n", argv[0], argv[1]);
fprintf (stderr, "options: -update <seconds>\n\t -font <font>\n");
exit (-1);
}
label = XtCreateManagedWidget ("label", labelWidgetClass, shell, NULL, 0);
get_update (XtDisplay (label));
timer (label, 0);
XtRealizeWidget (shell);
XtAppMainLoop (app);
}